﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class CarRecord
    {


        public int Add(Entity.CarRecord model)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    return dbContext.Insert<Entity.CarRecord>(model);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "添加车辆记录");
                    return 0;
                }
            }
        }
        public Entity.CarRecord Get(int id)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    return dbContext.Get<Entity.CarRecord>(id, null);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取车辆记录");
                    return null;
                }
            }
        }
        public Entity.CarRecord Get(string Guid)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    var precidate = new { Guid = Guid };
                    return dbContext.GetList<Entity.CarRecord>(precidate, null).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取车辆记录");
                    return null;
                }
            }
        }
        public List<Entity.CarRecord> Get(string filter, string OrderFields = "", bool isDesc = false)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    string sql = "select * from CarRecord";
                    if (filter.Length > 0)
                        sql += "  where  " + filter;
                    if (OrderFields.Length > 0)
                    {
                        if (isDesc)
                            sql += "   ORDER BY  " + OrderFields + "  DESC";
                        else
                            sql += "   ORDER BY  " + OrderFields;
                    }
                    return dbContext.GetListBySql<Entity.CarRecord>(sql, null).ToList();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取车辆记录");
                    return null;
                }
            }
        }
        public bool Dalete(int id)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    return dbContext.Delete<Entity.CarRecord>(id, null);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "删除车辆记录");
                    return false;
                }
            }
        }
        public bool Update(Entity.CarRecord model)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    return dbContext.Update<Entity.CarRecord>(model);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "更新车辆记录");
                    return false;
                }
            }
        }

      
    }
}
